"use strict"
const path = require("path")
const webpack = require("webpack")
function resolve(dir) {
  return path.join(__dirname, dir)
}
const port = process.env.port || process.env.npm_config_port || 9501
const ENV = process.env.ENV
const BACK_URL = process.env.BACK_URL
const LOGIN_URL = process.env.LOGIN_URL
console.log(LOGIN_URL)

const name = "公共卫生服务系统"
module.exports = {
  publicPath: "/",
  outputDir: "dist",
  assetsDir: "static",
  lintOnSave: false,
  devServer:
    ENV === "union"
      ? {
          port, // 端口号的配置
          open: true, // 自动打开浏览器
          proxy: {
            "/api/": {
              target: BACK_URL,
              ws: true,
              pathRewrite: {
                "^/api/": "/api/"
              }
            },
            "/basic/": {
              target: BACK_URL,
              ws: true,
              pathRewrite: {
                "^/basic/": "/basic/"
              }
            },
            "/upms/": {
              target: BACK_URL,
              ws: true,
              pathRewrite: {
                "^/upms/": "/upms/"
              }
            },
            "/his/": {
              target: BACK_URL,
              ws: true,
              pathRewrite: {
                "^/his/": "/his/"
              }
            },
            "/auth/": {
              target: BACK_URL,
              ws: true,
              pathRewrite: {
                "^/auth/": "/auth/"
              }
            }
          },
          overlay: {
            warnings: false,
            errors: true
          }
        }
      : {
          port, // 端口号的配置
          open: true, // 自动打开浏览器
          overlay: {
            warnings: false,
            errors: true
          },
          before: require("./mock/mock-server.js")
        },
  configureWebpack: {
    name: name,
    resolve: {
      alias: {
        "@": resolve("src")
      }
    },
    plugins: [
      new webpack.ProvidePlugin({
        $: "jquery"
      })
    ]
  },
  chainWebpack(config) {
    // 预加载 提高首屏的加载速度
    config.plugin("preload").tap(() => [
      {
        rel: "preload",
        fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
        include: "initial"
      }
    ])

    config.plugins.delete("prefetch")

    // 设置 svg-sprite-loader
    config.module
      .rule("svg")
      .exclude.add(resolve("src/icons"))
      .end()
    config.module
      .rule("icons")
      .test(/\.svg$/)
      .include.add(resolve("src/icons"))
      .end()
      .use("svg-sprite-loader")
      .loader("svg-sprite-loader")
      .options({
        symbolId: "icon-[name]"
      })
      .end()
  }
}
